package com.longrise.android.byjk.plugins.vip.myintegral;

import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.AppBarLayout;
import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.longrise.android.byjk.R;
import com.longrise.common.base.BaseActivity;
import com.longrise.android.byjk.event.MyIntegralEvent;
import com.longrise.android.byjk.plugins.publicWeb.PublicWebJumpUtil;
import com.longrise.android.byjk.plugins.vip.integralexchange.IntegralExchange2Activity;
import com.longrise.android.byjk.plugins.vip.integralexchange.IntegralTaskUtil;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;


/**
 * Created by callxkj on 2017/4/14.
 * 我的积分
 */

public class MyIntegralActivity extends BaseActivity<MyIntegralPresenter>
        implements MyIntegralContract.View, AppBarLayout.OnOffsetChangedListener,
        TabLayout.OnTabSelectedListener, View.OnClickListener {

    private Toolbar mToolbar;
    private AppBarLayout mAppbar;
    private TabLayout mTb;
    private ViewPager mVp;
    private TextView mTvuserscores;
    private LinearLayout mLLzqjf;
    private LinearLayout mLLjfgz;
    private LinearLayout mLLjfdh;

    @Override
    public int getContentViewId(@Nullable Bundle savedInstanceState) {
        return R.layout.activity_myintegral;
    }

    @Override
    public void initView() {
        mToolbar = (Toolbar) findViewById(R.id.myintegral_bb_toolbar);
        mToolbar.setTitle("");
        setSupportActionBar(mToolbar);
        mAppbar = (AppBarLayout) findViewById(R.id.myintegral_appbar);
        mTb = (TabLayout) findViewById(R.id.myintegral_tablayout);
        mVp = (ViewPager) findViewById(R.id.myintegral_vp);
        mTvuserscores = (TextView) findViewById(R.id.myintegral_userScores);
        mLLzqjf = (LinearLayout) findViewById(R.id.myintegral_zqjf_ll);
        mLLjfgz = (LinearLayout) findViewById(R.id.myintegral_jfgz_ll);
        mLLjfdh = (LinearLayout) findViewById(R.id.myintegral_jfdh_ll);
        initAdapter();
        initEvent();
        EventBus.getDefault().register(this);

    }

    @Override
    public void initTheme() {
        setTheme(R.style.AppCompatThemDark);
    }

    private void initAdapter() {
        MyIntegralPagerAdapter adapter = new MyIntegralPagerAdapter(getSupportFragmentManager());
        mVp.setOffscreenPageLimit(2);
        mVp.setAdapter(adapter);
        mTb.setupWithViewPager(mVp);
        for (int i = 0; i < adapter.getCount(); i++) {
            TabLayout.Tab tab = mTb.getTabAt(i);
            tab.setCustomView(R.layout.item_myintegral_tb);
            if (i == 0) {
                tabSelected(tab);
            }
        }
    }


    private void initEvent() {
        mLLzqjf.setOnClickListener(this);
        mLLjfgz.setOnClickListener(this);
        mLLjfdh.setOnClickListener(this);
        mAppbar.addOnOffsetChangedListener(this);
        mTb.addOnTabSelectedListener(this);
        mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    @Override
    public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) {
        if (verticalOffset == 0) {
            mToolbar.setBackgroundColor(Color.TRANSPARENT);
        } else if (Math.abs(verticalOffset) >= appBarLayout.getTotalScrollRange()) {
            mToolbar.setBackgroundResource(R.drawable.bg_act_my_coin);
        } else {
            mToolbar.setBackgroundResource(R.drawable.bg_act_my_coin);
        }
    }

    //-------------------TabLayout---------------
    @Override
    public void onTabSelected(TabLayout.Tab tab) {
        tabSelected(tab);

    }

    @Override
    public void onTabUnselected(TabLayout.Tab tab) {
        tabUnselected(tab);
    }

    @Override
    public void onTabReselected(TabLayout.Tab tab) {

    }
    //-------------------TabLayout---------------

    private void tabSelected(TabLayout.Tab tab) {
        TextView tv = (TextView) tab.getCustomView().findViewById(android.R.id.text1);
        tv.setBackgroundResource(R.drawable.bt_my_coin_detail);
        tv.setTextColor(Color.parseColor("#FF9600"));
        int position = tab.getPosition();
        mVp.setCurrentItem(position);

    }

    private void tabUnselected(TabLayout.Tab tab) {
        TextView tv = (TextView) tab.getCustomView().findViewById(android.R.id.text1);
        tv.setBackgroundColor(Color.TRANSPARENT);
        tv.setTextColor(Color.parseColor("#66000000"));
        mVp.setCurrentItem(tab.getPosition());
    }

    @Override
    public void setUserScores(String vipscores) {
        mTvuserscores.setText(vipscores);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.myintegral_zqjf_ll: //赚取积分
                IntegralTaskUtil.toIntegralTask(this);
                break;
            case R.id.myintegral_jfgz_ll:  //积分规则
               // PublicWebJumpUtil.jumpToPubWeb(this, "jfgz");
                break;
            case R.id.myintegral_jfdh_ll: //积分兑换
                startActivity(IntegralExchange2Activity.class);
                break;
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void getMyIntegralEvent(MyIntegralEvent event) {
        if (event.ismIsNeedRefresh()) {
            mPresenter.init();
        }
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}
